<?php

class CarDAO {

    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function create(ContactCar $car) {
        $db = $this->db;

        $stmt = $db->prepare("INSERT INTO `car` (model,mark,color,plaque ,year,dateCad) VALUES (?,?,?,?,?,?)");

        $model = $car->getModel();
        $stmt->bindParam(1, $model);

        $marck = $car->getMark();
        $stmt->bindParam(2, $marck);

        $color = $car->getColor();
        $stmt->bindParam(3, $color);

        $plaque = $car->getPlaque();
        $stmt->bindParam(4, $plaque);

        $year = $car->getYear();
        $stmt->bindParam(5, $year);

        $dateCad = $car->getDateCad();
        $stmt->bindParam(6, $dateCad);

        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function update(ContactCar $car) {
       $db = $this->db;
       $idCar=$car->getId();

        $stmt = $db->prepare("update car c set c.model=?,c.mark=?,c.color=?,c.plaque=? ,c.year=?,c.dateCad=? where id=$idCar");

        $model = $car->getModel();
        $stmt->bindParam(1, $model);

        $marck = $car->getMark();
        $stmt->bindParam(2, $marck);

        $color = $car->getColor();
        $stmt->bindParam(3, $color);

        $plaque = $car->getPlaque();
        $stmt->bindParam(4, $plaque);

        $year = $car->getYear();
        $stmt->bindParam(5, $year);

        $dateCad = $car->getDateCad();
        $stmt->bindParam(6, $dateCad);

        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function remove($id) {
       $db = $this->db;
       

        $stmt = $db->prepare("delete from car  where id=$id");
        $result = $stmt->execute();
        if ($result) {
            return true;
        }else
            return 0;
    }

    public function getById($id) {
        $contact = null;
        $db = $this->db;

        $stmt = $db->prepare("SELECT * FROM car WHERE	id = ?");
        $stmt->bindParam(1, $id);
        $stmt->execute();

        $row = $stmt->fetch();

        if (isset($row['id'])) {
            $car = new ContactCar();
            $car->setId($row['id']);
            $car->setModel($row['model']);
            $car->setMark($row['mark']);
            $car->setColor($row['color']);
            $car->setPlaque($row['plaque']);
            $car->setYear($row['year']);
            $car->setDateCad($row['dateCad']);
        }

        return $car;
    }
    
    
    
    public function getByPlaque($plaque) {
         $contact = null;
        $db = $this->db;

        $stmt = $db->prepare("SELECT * FROM car WHERE	plaque = ?");
        $stmt->bindParam(1, $plaque);
        $stmt->execute();

        $row = $stmt->fetch();
        $car=false;
        if (isset($row['id'])) {
         $car=true;
           
        } 

        return $car; 
    }
    

    public function getList($model) {
        
            $cars = array();
            $db = $this->db;
            if($model == ""){
               $stmt = $db->prepare("SELECT * FROM car"); 
            }else{
                   $stmt = $db->prepare("SELECT * FROM car where model like'%$model%'");
                   }
            $stmt->execute();

            $result = $stmt->fetchAll();

            if (sizeof($result) > 0) {
                foreach ($result as $row) {

                    $car = new ContactCar();
                    $car->setId($row['id']);
                    $car->setModel($row['model']);
                    $car->setMark($row['mark']);
                    $car->setColor($row['color']);
                    $car->setPlaque($row['plaque']);
                    $car->setYear($row['year']);
                    $car->setDateCad($row['dateCad']);
                    $cars[] = $car;
                }
                return $cars;
            }else return false;

            
        
    }

}

?>